/*
 Copyright 2014 Alexander V. Nikulin
 
 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at
	
 	http://www.apache.org/licenses/LICENSE-2.0
	
 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
*/
package ru.avn.adf;

import java.lang.annotation.Annotation;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;


/**
 * @author Alexander V. Nikulin
 */
public class AdfExecutorConfiguration {

	private Map<Class<Annotation>, Class<PayloadProcessor<Annotation>>> bindings;	
	
	private Class<AdfExecutorDecorator> executorDecoratorType;
	
	private Class<PayloadProcessorDecorator> processorDecoratorType;
	
	private boolean isStopOnException;
		
	private AdfExecutorConfiguration(Builder builder) {
		this.bindings = builder.bindings;
		this.executorDecoratorType = builder.executorDecoratorType;
		this.isStopOnException = builder.isStopOnException;
		this.processorDecoratorType = builder.processorDecoratorType;
	}

	public static Builder newBuilder() {
		return new Builder();
	}
	
	public static class Builder {
			
		private Builder() {
		}

		private Map<Class<Annotation>, Class<PayloadProcessor<Annotation>>> bindings = Collections.emptyMap();	
		
		private Class<AdfExecutorDecorator> executorDecoratorType;
		
		private Class<PayloadProcessorDecorator> processorDecoratorType;
		
		private boolean isStopOnException = true;
		
		@SuppressWarnings("unchecked")
		public <A extends Annotation> Builder bind(Class<A> annotationType
				, Class<? extends PayloadProcessor<A>> processorType) {
			
			if (annotationType == null || processorType == null) {
				throw new IllegalArgumentException("annotationType is null or processorType is null");
			}
			 
			if (bindings.isEmpty()) {
				bindings = createMap();
			}
				
			bindings.put((Class<Annotation>) annotationType, (Class<PayloadProcessor<Annotation>>) (Class<? extends PayloadProcessor<Annotation>>) processorType);
			
			return this;
		}
	
		@SuppressWarnings("unchecked")
		public Builder setExecutorDecorator(Class<? extends AdfExecutorDecorator> executorDecoratorType) {
			this.executorDecoratorType = (Class<AdfExecutorDecorator>) executorDecoratorType;
			return this;
		}
		
		@SuppressWarnings("unchecked")
		public Builder setPayloadProcessorDecorator(Class<? extends PayloadProcessorDecorator> processorDecoratorType) {
			this.processorDecoratorType = (Class<PayloadProcessorDecorator>) processorDecoratorType;
			return this;
		}
		
		public Builder setIsStopOnException(boolean value) {
			this.isStopOnException = value;
			return this;
		}
		
		public AdfExecutorConfiguration build() {
			
			if (bindings.isEmpty()) {
				throw new IllegalStateException("bindings is empty");
			}
		
			return new AdfExecutorConfiguration(this);
		}
	
	}
	
	public boolean isAnnotationBinded(Class<? extends Annotation> annotationType) {
		return bindings.containsKey(annotationType);
	}
	
	public Collection<Class<Annotation>> getDeclaredAnnotations() {
		return bindings.keySet();
	}

	public Class<PayloadProcessor<Annotation>> getPayloadProcessor(Class<? extends Annotation> annotationType) {
		return bindings.get(annotationType);
	}
		
	public Class<AdfExecutorDecorator> getExecutorDecorator() {
		return executorDecoratorType;
	}

	public boolean isStopOnException() {
		return isStopOnException;
	}
	
	private static Map<Class<Annotation>, Class<PayloadProcessor<Annotation>>> createMap() {
		return new HashMap<Class<Annotation>, Class<PayloadProcessor<Annotation>>>();
	}

	public Class<PayloadProcessorDecorator> getProcessorDecorator() {
		return processorDecoratorType;
	}
	
}
